package net.shopxx.entity;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.Transient;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

/**
 * 实体类 - 退款
 * 
 * requested-->processed(agree, refused-->[platform,closed])
 * --timeout(3)-->platform-->completed
 */

@Entity
public class Refund extends BaseEntity {

	private static final long serialVersionUID = -2533117666249761057L;

	public enum RefundStatus {
		requested, processed, platformed, completed, closed,canceled
	};

	public enum ShippingStatus {
		noshipping,unshipped, shipped, confirmed
	};

	public enum ProcessStatus {
		unprocessed,saler_agreed, saler_refused, platform_joined,platform_agreed,platform_refused, confirmed,buyer_cancel
	};

	public enum PlatOperation {
		goodsIssue,noReason, noReceived
	};

	public enum RefundType {
		goodsIssue,senvendays, noReceived
	};

	public enum RefundReq {
		retGoodsAndCache, exchange
	};

	public enum GoodsStatus {
		noRecevied, recevied;
	};
	
	private String refundSn;
	private RefundStatus refundStatus;
	private ShippingStatus shippingStatus;
	private ProcessStatus processStatus;
	private GoodsStatus goodsStatus;
	private Boolean isSelfSign;//
	private Receiver receiver;
	
	private String deliveryCorpName;
	private String shippingNo;
	private RefundType refundType;
	private RefundReq refundReq;
	private String cellnumber;
	private String operator;// 操作员
	private String memo; 
	 
	private String salerReason;  
	private String platformReason; 
	
	private String dealRet; 
	private String dealDesc; 
	private String dealRule; 
	
	private Order order;// 订单

	private Set<Refundnote> refundnoteSet;
	private Set<Refundproof> refundproofSet;

	@Column(nullable = false, updatable = false, unique = true)
	public String getRefundSn() {
		return refundSn;
	}

	public void setRefundSn(String refundSn) {
		this.refundSn = refundSn;
	}

	@Column(updatable = false)
	public String getOperator() {
		return operator;
	}

	public void setOperator(String operator) {
		this.operator = operator;
	}

	@Column(updatable = false, length = 5000)
	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public String getCellnumber() {
		return cellnumber;
	}

	public void setCellnumber(String cellnumber) {
		this.cellnumber = cellnumber;
	}

	public RefundStatus getRefundStatus() {
		return refundStatus;
	}

	public void setRefundStatus(RefundStatus refundStatus) {
		this.refundStatus = refundStatus;
	}

	public ShippingStatus getShippingStatus() {
		return shippingStatus;
	}

	public void setShippingStatus(ShippingStatus shippingStatus) {
		this.shippingStatus = shippingStatus;
	}

	public ProcessStatus getProcessStatus() {
		return processStatus;
	}

	public void setProcessStatus(ProcessStatus processStatus) {
		this.processStatus = processStatus;
	}
	

	public RefundType getRefundType() {
		return refundType;
	}

	public void setRefundType(RefundType refundType) {
		this.refundType = refundType;
	}

	@OneToMany(mappedBy = "refund", fetch = FetchType.LAZY)
	@Cascade(value = { CascadeType.DELETE,CascadeType.SAVE_UPDATE })
	@OrderBy("createDate desc")
	public Set<Refundnote> getRefundnoteSet() {
		return refundnoteSet;
	}

	public void setRefundnoteSet(Set<Refundnote> refundnoteSet) {
		this.refundnoteSet = refundnoteSet;
	}

	@OneToMany(mappedBy = "refund", fetch = FetchType.LAZY)
	@Cascade(value = { CascadeType.DELETE_ORPHAN,CascadeType.SAVE_UPDATE })
	@OrderBy("createDate desc")
	public Set<Refundproof> getRefundproofSet() {
		return refundproofSet;
	}

	public void setRefundproofSet(Set<Refundproof> refundproofSet) {
		this.refundproofSet = refundproofSet;
	}

	@Transient
	public String getRefundStatusDesc() {
		String sb = null;
		switch (this.refundStatus) {
		case requested:
			sb="请求维权";
			break;
		case processed:
			sb="维权处理中";
			if (this.processStatus != null) {
				switch (this.processStatus) {
				case unprocessed:
					sb="等待卖家处理";
					break;
				case saler_agreed:
					sb="卖家已同意";
					switch (this.shippingStatus) {
					case noshipping:
						if(this.goodsStatus.equals(GoodsStatus.noRecevied))
							sb ="等待卖家退款";
						break;
					case unshipped:
						if(this.getReceiver()==null)
							sb ="等待卖家确认收货地址";
						else
							sb ="等待买家退货";
						break;
					case shipped:
						sb ="等待卖家收货";
						break;
					case confirmed:
						sb ="卖家确认收货";
						break;
					}
					break;
				case saler_refused:
					sb="卖家不同意";
					break;
				case confirmed:
					sb="买家已确认退款";
					break;
				}
			}
			break;
		case platformed:
			if (this.processStatus != null) {
				switch (this.processStatus) {
				case platform_joined:
					sb="等待平台处理";
					break;
				case platform_agreed:
					sb="平台已同意";
					switch (this.shippingStatus) {
					case noshipping:
						if(this.goodsStatus.equals(GoodsStatus.noRecevied))
							sb ="等待卖家退款";
						break;
					case unshipped:
						if(this.getReceiver()==null)
							sb ="等待卖家确认收货地址";
						else
							sb ="等待买家退货";
						break;
					case shipped:
						sb ="等待卖家收货";
						break;
					case confirmed:
						sb ="卖家确认收货";
						break;
					}
					break;
				case platform_refused:
					sb="平台不同意";
					break;
				case confirmed:
					sb="买家已确认退款";
					break;
				}
			}
			break;
		case completed:
			sb="维权完成";
			break;
		case closed:
			sb="维权关闭";
			break;
		case canceled:
			sb="买家取消维权";
		default:
			break;
		}
		return sb;
	}
	
	@Transient
	public String getDealDescStr() {
		String sb = null;
		switch (this.refundStatus) {
		case processed:
			sb="维权处理中";
			if (this.processStatus != null) {
				switch (this.processStatus) {
				case unprocessed:
					sb="卖家尚未处理";
					break;
				case saler_agreed:
					sb="卖家已同意";
					switch (this.shippingStatus) {
					case noshipping:
						if(this.goodsStatus.equals(GoodsStatus.noRecevied))
							sb ="等待卖家退款";
						break;
					case unshipped:
						if(this.getReceiver()==null)
							sb ="等待卖家确认收货地址";
						else
							sb ="等待买家退货";
						break;
					case shipped:
						sb ="等待卖家收货";
						break;
					case confirmed:
						sb ="卖家确认收货";
						break;
					}
					break;
				case saler_refused:
					sb="卖家不同意";
					break;
				case platform_joined:
					sb="平台介入处理";
					break;
				case platform_agreed:
					sb="平台同意退款";
					break;
				case platform_refused:
					sb="平台不同意退款";
					break;
				case confirmed:
					sb="买家已确认退款";
					break;
				}
			}
			break;

		case platformed:
			if (this.processStatus != null) {
				switch (this.processStatus) {
				case platform_joined:
					sb="平台介入处理";
					break;
				case platform_agreed:
					sb="平台同意退款";
					switch (this.shippingStatus) {
					case noshipping:
						if(this.goodsStatus.equals(GoodsStatus.noRecevied))
							sb ="等待卖家退款";
						break;
					case unshipped:
						if(this.getReceiver()==null)
							sb ="等待卖家确认收货地址";
						else
							sb ="等待买家退货";
						break;
					case shipped:
						sb ="等待卖家收货";
						break;
					case confirmed:
						sb ="卖家确认收货";
						break;
					}
					break;
				case platform_refused:
					sb="平台同意退款";
					break;
				case confirmed:
					sb="买家已确认退款";
					break;
				}
			}
			break;
		case completed:
			sb="维权完成";
			break;
		case closed:
			sb="维权关闭";
			break;
		case canceled:
			sb="买家取消维权";
		default:
			break;
		}
		return sb;
	}

	@OneToOne(fetch=FetchType.LAZY)
	@Cascade(value = { CascadeType.DELETE_ORPHAN,CascadeType.SAVE_UPDATE })
	public Receiver getReceiver() {
		return this.receiver;
	}

	public void setReceiver(Receiver receiver) {
		this.receiver = receiver;
	}

	public GoodsStatus getGoodsStatus() {
		return goodsStatus;
	}

	public void setGoodsStatus(GoodsStatus goodsStatus) {
		this.goodsStatus = goodsStatus;
	}

	public Boolean getIsSelfSign() {
		return isSelfSign;
	}

	public void setIsSelfSign(Boolean isSelfSign) {
		this.isSelfSign = isSelfSign;
	}

	public RefundReq getRefundReq() {
		return refundReq;
	}

	public void setRefundReq(RefundReq refundReq) {
		this.refundReq = refundReq;
	}

	public String getDeliveryCorpName() {
		return deliveryCorpName;
	}

	public String getShippingNo() {
		return shippingNo;
	}

	public void setDeliveryCorpName(String deliveryCorpName) {
		this.deliveryCorpName = deliveryCorpName;
	}

	public void setShippingNo(String shippingNo) {
		this.shippingNo = shippingNo;
	}

	public String getDealRet() {
		return dealRet;
	}

	public void setDealRet(String dealRet) {
		this.dealRet = dealRet;
	}

	public String getDealRule() {
		return dealRule;
	}

	public void setDealRule(String dealRule) {
		this.dealRule = dealRule;
	}

	public String getDealDesc() {
		return dealDesc;
	}

	public void setDealDesc(String dealDesc) {
		this.dealDesc = dealDesc;
	}

	public String getSalerReason() {
		return salerReason;
	}

	public String getPlatformReason() {
		return platformReason;
	}

	public void setSalerReason(String salerReason) {
		this.salerReason = salerReason;
	}

	public void setPlatformReason(String platformReason) {
		this.platformReason = platformReason;
	}

}